//
//  NextViewController.m
//  YinYueBoFang_Forever
//
//  Created by lanou3g on 15/11/5.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "NextViewController.h"
#import "NextView.h"
#import "MusicModel.h"
#import "PlayerHelpers.h"
#import "DataHandle.h"
#import "UIImageView+WebCache.h"
#import "LyricHelpers.h"
@interface NextViewController ()<PlayerHelpersDelegate,UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NextView *nv;
@property(nonatomic,strong)MusicModel *model;


@end

@implementation NextViewController



-(void)loadView
{
    self.nv = [[NextView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.nv;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.nv.tab.delegate = self;
    self.nv.tab.dataSource = self;
    [self.nv.tab registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.nv.backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [PlayerHelpers defaultPlayers].delegate = self;
    
    MusicModel *model = [[DataHandle defaultDataHandle] musicModelWithindex:self.index];
    [self.nv.lastButton addTarget:self action:@selector(lastButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.nv.controButton addTarget:self action:@selector(controButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.nv.nextButton addTarget:self action:@selector(nextButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.nv.slider addTarget:self action:@selector(sliderAction:) forControlEvents:(UIControlEventValueChanged)];
    
    [self.nv.danqu addTarget:self action:@selector(danquAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.nv.xunhuan addTarget:self action:@selector(xunhuanAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    [self.nv.suiji addTarget:self action:@selector(suijiAction:) forControlEvents:(UIControlEventTouchUpInside)];

    if (![[PlayerHelpers defaultPlayers] isCurrentMusicWithUrlString:model.mp3Url]) {
        self.model = model;
    }else
    {
        [self changeUiWitgModel:model];
    }
 
}

-(void)danquAction:(UIButton *)sender
{
    NSLog(@"%ld",self.nv.danqu.tag - 10000);
    [PlayerHelpers defaultPlayers].playStatus = self.nv.danqu.tag - 10000;
}

-(void)xunhuanAction:(UIButton *)sender
{
    NSLog(@"%ld",self.nv.xunhuan.tag - 10000);
    [PlayerHelpers defaultPlayers].playStatus = self.nv.xunhuan.tag - 10000;
}

-(void)suijiAction:(UIButton *)sender
{
    NSLog(@"%ld",self.nv.suiji.tag - 10000);
    [PlayerHelpers defaultPlayers].playStatus = self.nv.suiji.tag - 10000;
}

-(void)sliderAction:(UISlider *)sender
{
    [[PlayerHelpers defaultPlayers] seekToTime:sender.value];
}



-(void)lastButtonAction:(UIButton *)sender
{
    if (self.index == 0) {
        self.index = [[DataHandle defaultDataHandle] musicModelCount]-1;
    }else
    {
        self.index-=1;
        
    }
    self.model = [[DataHandle defaultDataHandle] musicModelWithindex:self.index];
}

-(void)controButtonAction:(UIButton *)sender
{
    if ([[PlayerHelpers defaultPlayers] isPlaying]) {
        [[PlayerHelpers defaultPlayers] pause];
        [self.nv.controButton setTitle:@"播放" forState:(UIControlStateNormal)];
                                                       
    }else
    {
        [[PlayerHelpers defaultPlayers] play];
        [self.nv.controButton setTitle:@"暂停" forState:(UIControlStateNormal)];
    }
        
                                                      
}
-(void)nextButtonAction:(UIButton *)sender
{
    switch ([PlayerHelpers defaultPlayers].playStatus) {
        case 2:
            if (self.index == [[DataHandle defaultDataHandle] musicModelCount]-1) {
                self.index = 0;
            }else
            {
                self.index +=1;
            }
            self.model = [[DataHandle defaultDataHandle] musicModelWithindex:self.index];
            break;
        case 1:self.index = arc4random() % [[DataHandle defaultDataHandle] musicModelCount];
            self.model = [[DataHandle defaultDataHandle] musicModelWithindex:self.index];
            case 0:
            self.model = [[DataHandle defaultDataHandle] musicModelWithindex:self.index];
            break;
        default:
            break;
    }
}


-(void)backButtonAction:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

-(void)setModel:(MusicModel *)model
{
    _model = model;
    [[PlayerHelpers defaultPlayers] playWithURL:self.model.mp3Url];
    
    [self changeUiWitgModel:model];
    
}

-(void)changeUiWitgModel:(MusicModel *)model
{
    
    [[LyricHelpers defaultLyricHelpers] changeLyricString:model.lyric];
    self.nv.songNameLable.text = model.name;
    self.nv.singerLable.text = model.singer;
    [self.nv.v2 sd_setImageWithURL:[NSURL URLWithString:model.picUrl]];
    self.nv.slider.value = 0;
    self.nv.slider.minimumValue = 0;
    float musicTime = [model.duration floatValue]/1000;
    self.nv.slider.maximumValue = musicTime;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}






#pragma mark --playerHelpersDelegate
-(void)musicPlayTime:(float)time
{
    self.nv.slider.value = time;
    self.nv.v2.layer.transform = CATransform3DRotate(self.nv.v2.layer.transform, M_PI/100, 0, 0, 1);
    
    NSInteger index = [[LyricHelpers defaultLyricHelpers] lyricItemWithTime:time];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    
    [self.nv.tab selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
    
    
    
}


- (void)dealloc
{
    [PlayerHelpers defaultPlayers].delegate = nil;
}

-(void)currentMusicDidFinish
{
    [self nextButtonAction:nil];
}


#pragma mark --tableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[LyricHelpers defaultLyricHelpers] lyricItemCount];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self.nv.tab dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = [[LyricHelpers defaultLyricHelpers] lyricStringWithIndex:indexPath.row];
    
    
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.highlightedTextColor = [UIColor blueColor];
    
    return cell;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
